use([
    "public/javascripts/service/notification_infoService.js",
    "public/javascripts/widget/slider/slider.js",
    "public/javascripts/widget/prompt/prompt.js"
], function(notification_infoService, SliderComponent, PromptComponent) {
    var PageComponent = React.createClass({
        isLock: false,
        restData:{
            'refAskBoardId':0,
            'pageIndex':1,
            'pageSize':10
        },
        componentDidMount: function() {
            $('#m-msg').scrollTop($('#m-msg')[0].scrollHeight);
        },
        receiveMsg: function(data) {

        },
        getInitialState: function () {
            return this.props.data ? this.props.data : {};
        },
        render:function() {
            var data = this.state.data;

            var  _this = this,
                Browser = typeof window != 'undefined',
                list = this.state.list ? this.state.list : [],
                totalResult = this.state.totalResult ? this.state.totalResult : 0;

            _this.restData.refAskBoardId = this.state.refAskBoardId;
            return ~init;
        },
        handleScroll: function(event){
            var _this = this;

            if(_this.isLock){
                return ;
            }

            if(event.target.scrollTop == 0){
                if(_this.restData.pageIndex > _this.state.totalPage){
                    return ;
                }

                _this.isLock = true;

                //从第二页开始加载数据
                _this.restData.pageIndex == 1 ? ++_this.restData.pageIndex : "";

                var config = {
                    urlParams:_this.restData
                };

                //console.log(_this.state.list);
                notification_infoService.getMsgList(config, function(res){
                    if(res.errorCode === 0){
                        ++_this.restData.pageIndex;
                        for(var i=0,l=res.data.length; i<l; i++){
                            _this.state.list.unshift(res.data[i]);
                        }
                        _this.setState();
                    }
                    setTimeout(function(){$('#m-msg').scrollTop($('#m-msg').height());_this.isLock = false;}, 100);
                });
            }
        },
        sendMsg: function(){
            var _this = this;

            var msgcontent = $.trim($('#msgcontent').val());
            if(!msgcontent){
                return;
            }

            var config = {
                urlParams:{
                    'refAskBoardId':_this.state.refAskBoardId,
                    'messageBody':msgcontent
                }
            };
            notification_infoService.sendMsg(config, function(res){
                if(res.errorCode === 0){
                    _this.state.list.push(res.data);
                    _this.setState();
                    $('#msgcontent').val('');
                    $('#m-msg').scrollTop($('#m-msg')[0].scrollHeight);
                }else{
                    PromptComponent.init({'type':'warning','msg':res.msg}).show();
                }
            });
        }
    });
    if(typeof ReactDOM != "undefined") {
        ReactDOM.render(<PageComponent data={storage}/>, document.getElementById('container'));
    }
    module.exports = PageComponent;
});